﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using ASP;
using ASP.Models;

namespace ASP.Controllers
{
    /*
    The WebApiConfig class may require additional changes to add a route for this controller. Merge these statements into the Register method of the WebApiConfig class as applicable. Note that OData URLs are case sensitive.

    using System.Web.Http.OData.Builder;
    using System.Web.Http.OData.Extensions;
    using ASP;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<ProductOut>("ProductOuts");
    builder.EntitySet<Member>("Members"); 
    builder.EntitySet<ProductDB>("ProductDBs"); 
    config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    */
    public class ProductOutsController : ODataController
    {
        private productOutContext db = new productOutContext();

        // GET: odata/ProductOuts
        [EnableQuery]
        public IQueryable<ProductOut> GetProductOuts()
        {
            return db.ProductOuts;
        }

        // GET: odata/ProductOuts(5)
        [EnableQuery]
        public SingleResult<ProductOut> GetProductOut([FromODataUri] int key)
        {
            return SingleResult.Create(db.ProductOuts.Where(productOut => productOut.ProOut_Id == key));
        }

        // PUT: odata/ProductOuts(5)
        public async Task<IHttpActionResult> Put([FromODataUri] int key, Delta<ProductOut> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ProductOut productOut = await db.ProductOuts.FindAsync(key);
            if (productOut == null)
            {
                return NotFound();
            }

            patch.Put(productOut);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductOutExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(productOut);
        }

        // POST: odata/ProductOuts
        public async Task<IHttpActionResult> Post(ProductOut productOut)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.ProductOuts.Add(productOut);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (ProductOutExists(productOut.ProOut_Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return Created(productOut);
        }

        // PATCH: odata/ProductOuts(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<ProductOut> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ProductOut productOut = await db.ProductOuts.FindAsync(key);
            if (productOut == null)
            {
                return NotFound();
            }

            patch.Patch(productOut);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductOutExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(productOut);
        }

        // DELETE: odata/ProductOuts(5)
        public async Task<IHttpActionResult> Delete([FromODataUri] int key)
        {
            ProductOut productOut = await db.ProductOuts.FindAsync(key);
            if (productOut == null)
            {
                return NotFound();
            }

            db.ProductOuts.Remove(productOut);
            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }

        // GET: odata/ProductOuts(5)/Member
        [EnableQuery]
        public SingleResult<Member> GetMember([FromODataUri] int key)
        {
            return SingleResult.Create(db.ProductOuts.Where(m => m.ProOut_Id == key).Select(m => m.Member));
        }

        // GET: odata/ProductOuts(5)/ProductDB
        [EnableQuery]
        public SingleResult<ProductDB> GetProductDB([FromODataUri] int key)
        {
            return SingleResult.Create(db.ProductOuts.Where(m => m.ProOut_Id == key).Select(m => m.ProductDB));
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool ProductOutExists(int key)
        {
            return db.ProductOuts.Count(e => e.ProOut_Id == key) > 0;
        }
    }
}
